﻿using System;
using System.Collections.Generic;
using System.Linq;

namespace IntellectShop.Common
{
	public partial class Commodity
	{
		public static Common.Commodity RetrieveByID(int id)
		{
			using (DB.IntellectShopDataContext context = new DB.IntellectShopDataContext(Globals.ConnectionString))
			{
				var query = context.Commodities.SingleOrDefault(c => c.CommodityID == id);

				if (query == null)
					return null;

				return new Common.Commodity
				{
					Code = query.Code,
					CommodityID = query.CommodityID,
					CommodityType = query.CommodityType.HasValue ? (CommodityType)query.CommodityType.Value : CommodityType.NotSpecified,
					Description = query.Descr,
					Name = query.Name,
					Price = new Common.Money { Amount = (query.Price.HasValue ? query.Price.Value : 0), Currency = Currency.RUB },
					SortingID = query.SortingID,
					Created = query.Created,
					CreatedBy = query.CreatedBy,
					Updated = query.Updated,
					UpdatedBy = query.UpdatedBy
				};
			}
		}

		public static Common.Commodity RetrieveByCode(string code)
		{
			using (DB.IntellectShopDataContext context = new DB.IntellectShopDataContext(Globals.ConnectionString))
			{
				var query = context.Commodities.SingleOrDefault(c => c.Code == code);

				if (query == null)
				{
					return null;
				}

				return new Common.Commodity
				{
					Code = query.Code,
					CommodityID = query.CommodityID,
					CommodityType = query.CommodityType.HasValue ? (CommodityType)query.CommodityType.Value : CommodityType.NotSpecified,
					Description = query.Descr,
					Name = query.Name,
					Price = new Common.Money { Amount = (query.Price.HasValue ? query.Price.Value : 0), Currency = Currency.RUB },
					SortingID = query.SortingID,
					Created = query.Created,
					CreatedBy = query.CreatedBy,
					Updated = query.Updated,
					UpdatedBy = query.UpdatedBy
				};
			}
		}

		public static List<Common.Commodity> RetrieveAll()
		{
			using (DB.IntellectShopDataContext dataContext = new DB.IntellectShopDataContext(Globals.ConnectionString))
			{
				var query =
							from commodities in dataContext.Commodities
							orderby commodities.SortingID descending, commodities.CommodityID
							select commodities;

				List<Common.Commodity> result = new List<Commodity>();
				foreach (var row in query)
				{
					result.Add(new Common.Commodity
						{
							Code = row.Code,
							CommodityID = row.CommodityID,
							CommodityType = row.CommodityType.HasValue ? (CommodityType)row.CommodityType.Value : CommodityType.NotSpecified,
							Description = row.Descr,
							Name = row.Name,
							Price = new Common.Money { Amount = (row.Price.HasValue ? row.Price.Value : 0), Currency = Currency.RUB },
							SortingID = row.SortingID,
							Created = row.Created,
							CreatedBy = row.CreatedBy,
							Updated = row.Updated,
							UpdatedBy = row.UpdatedBy
						});
				}

				return result;
			}
		}

		public void Create(string createdBy)
		{
			using (DB.IntellectShopDataContext dataContext = new DB.IntellectShopDataContext(Common.Globals.ConnectionString))
			{
				var commodityDB = new DB.Commodity
				{
					Code = this.Code,
					Descr = this.Description,
					Name = this.Name,
					Price = this.Price.Amount,
					SortingID = this.SortingID,
					Created = DateTime.Now,
					CreatedBy = createdBy,
					CommodityType = (int)this.CommodityType
				};
				dataContext.Commodities.InsertOnSubmit(commodityDB);
				dataContext.SubmitChanges();
			}
		}

		public void Update(string updateBy)
		{
			using (DB.IntellectShopDataContext dataContext = new DB.IntellectShopDataContext(Common.Globals.ConnectionString))
			{
				var commodityDB = dataContext.Commodities.SingleOrDefault(x => x.CommodityID == this.CommodityID);
				commodityDB.SortingID = this.SortingID;
				commodityDB.Name = this.Name;
				commodityDB.Code = this.Code;
				commodityDB.Descr = this.Description;
				commodityDB.Price = this.Price.Amount;
				commodityDB.Updated = DateTime.Now;
				commodityDB.UpdatedBy = updateBy;

				dataContext.SubmitChanges();
			}
		}

		public static void Delete(int commodityID)
		{
			using (DB.IntellectShopDataContext dataContext = new DB.IntellectShopDataContext(Common.Globals.ConnectionString))
			{
				var commodityDB = (from commodity in dataContext.Commodities where commodity.CommodityID == commodityID select commodity).SingleOrDefault();
				dataContext.Commodities.DeleteOnSubmit(commodityDB);
				dataContext.SubmitChanges();
			}
		}
	}
}
